package com.zhuhao.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;

import java.util.Collections;
import java.util.List;

/**
 * 集合工具类
 *
 * @author haozhu7
 * @date 2023/9/8
 */
@Slf4j
public class ListUtil {

    public static <T, F> List<F> listCopyToAnotherList(List<T> sourceList, Class<F> target) {
        if (CollectionUtils.isEmpty(sourceList)) {
            return Collections.emptyList();
        }
        List<F> targetList = Lists.newArrayList();
        for (T source : sourceList) {
            try {
                F f = target.getDeclaredConstructor().newInstance();
                BeanUtils.copyProperties(source, f);
                targetList.add(f);
            } catch (Exception e) {
                log.info("集合转化错误！ {} --> {}, {}", sourceList, target, e.getMessage());
            }
        }
        return targetList;
    }
}
